package com.his.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.his.common.Const;
import com.his.common.PageParam;
import com.his.common.Rsp;
import com.his.common.Temp;
import com.his.entity.*;
import com.his.mapper.ApplicationMapper;
import com.his.service.ApplicationService;
import com.his.service.PatientService;
import com.his.service.RecordService;
import com.his.utils.JWTUtil;
import com.his.utils.MyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Application)表服务实现类
 *
 * @author makejava
 * @since 2020-10-23 18:04:36
 */
@Service("applicationService")
@Transactional(propagation = Propagation.REQUIRED)
public class ApplicationServiceImpl implements ApplicationService {
    @Resource
    private ApplicationMapper applicationMapper;

    @Autowired
    private RecordService recordService;

    @Resource
    private PatientService patientService;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Application queryById(Integer id) {
        return this.applicationMapper.queryById(id);
    }


    /**
     * 查询所有通过实体类属性     *
     *
     * @param application 实例对象
     * @return 查询的结果
     */
    @Override
    public List<Application> queryAll(Application application) {
        return this.applicationMapper.queryAll(application);
    }

    /**
     * 新增数据
     *
     * @param application 实例对象
     * @return 实例对象
     */
    @Override
    public Application insert(Application application) {
        this.applicationMapper.insert(application);
        return application;
    }

    /**
     * 修改数据
     *
     * @param application 实例对象
     * @return 实例对象
     */
    @Override
    public Application update(Application application) {
        this.applicationMapper.update(application);
        return this.queryById(application.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.applicationMapper.deleteById(id) > 0;
    }


    /**
     * 查询所有申请列表带分页
     *
     * @param pageParam
     * @return
     */
    @Override
    public Rsp queryAllApply(PageParam<Application> pageParam, HisUser user) {
        //开启分页
        PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
        for (int i = 0; i < pageParam.getOrderParams().length; i++) {
            PageHelper.orderBy(pageParam.getOrderParams()[i]);
        }
        Application model = pageParam.getModel();
        if (user.getRoles().contains("doctor")) {
            model.setDoctorId(user.getId());
        } else if (user.getRoles().contains("nurse")) {
            model.setNurseId(user.getId());
        }
        List<Application> applicationList = applicationMapper.queryAll(model);
        //设置pagebean对象
        PageInfo<Application> pageInfo = new PageInfo<>(applicationList);
        //使用响应实体返回对象
        return Rsp.success(pageInfo);
    }


    /**
     * 添加一条申请信息
     *
     * @param application
     * @return
     */
    @Override
    public Rsp insertOne(String token, Application application) throws Exception {
        Object obj = JWTUtil.parserToken(token, Const.PUBLIC_KEY);
        Patient patient = (Patient) obj;
        Record record = recordService.selectOneByPatientId(patient.getId().toString(), "");
        if (StringUtils.isEmpty(record)) {
            return Rsp.fail().msg("缺少病历信息");
        }
        //判断当前是否有住院申请正在申请中
        int flag = applicationMapper.getByPatientId(patient.getId());
        if (flag > 0) {
            //有的话返回
            return Rsp.fail().msg("当前已有正在申请中的记录");
        }
        application.setRecordId(record.getId());
        application.setPatientId(patient.getId());
        application.setNo(MyUtil.getNo());
        applicationMapper.insert(application);
        return Rsp.success(null);
    }

    @Override
    public Rsp updateOne(Application application) {
        applicationMapper.update(application);
        return Rsp.success(null);
    }


    @Override
    public Application selectOneByPatientId(String patientId) {
        Application application = applicationMapper.queryByPatientId(patientId);
        return application;
    }

    /**
     * 后端人员添加住院申请的方法
     *
     * @param temp 临时对象,其中有病人信息,病历信息,申请信息
     * @return 返回状态
     */
    @Override
    public Rsp insertByTemp(Temp temp) {
        //先添加病人
        patientService.insert(temp.getPatient());
        //添加病历
        //设置病历的病人id
        temp.getRecord().setPatientId(temp.getPatient().getId());
        recordService.insert(temp.getRecord());
        //添加申请
        //设置申请表的病历id和病人id
        Application application = temp.getApplication();
        application.setPatientId(temp.getPatient().getId());
        application.setRecordId(temp.getRecord().getId());
        application.setNo(MyUtil.getNo());
        applicationMapper.insert(application);
        return Rsp.success(null);
    }

    /**
     * 根据病人住院编号查询
     *
     * @param no 住院编号
     * @return 查询到的数据
     */
    @Override
    public Rsp getByNo(String no) {
        return Rsp.success(applicationMapper.getByNo(no));
    }

    @Override
    public PageInfo<ApplicantInfo> getAllApplicantInfo(PageParam<ApplicantInfo> applicantInfoPageParam, HisUser user) {
        PageHelper.startPage(applicantInfoPageParam.getPageNum(), applicantInfoPageParam.getPageSize());
        ApplicantInfo model = applicantInfoPageParam.getModel();
        if(user.getRoles().contains("nurse")){
            model.setRecordId(user.getId());
        }
        List<ApplicantInfo> applicantInfoList = applicationMapper.queryAllAllApplicantInfo(applicantInfoPageParam.getModel());
        return  new PageInfo<>(applicantInfoList);
    }

    @Override
    public PageInfo<ApplicantInfo> getAllApplicantInfoByAdvice(PageParam<ApplicantInfo> applicantInfoPageParam, HisUser user) {
        PageHelper.startPage(applicantInfoPageParam.getPageNum(), applicantInfoPageParam.getPageSize());
        ApplicantInfo model = applicantInfoPageParam.getModel();
        if(user.getRoles().contains("nurse")){
            model.setRecordId(user.getId());
        }
        List<ApplicantInfo> applicantInfoList = applicationMapper.queryAllAllApplicantInfoByAdvice(applicantInfoPageParam.getModel());
        return  new PageInfo<>(applicantInfoList);
    }

    @Override
    public List<Application> getByPatientId(int id) {
        Application a = new Application();
        a.setPatientId(id);
        return applicationMapper.queryAll(a);
    }
}
